#include <windows.h>
#include <mmsystem.h>
#include <stdio.h>


void main()
{
	int joyID = -1;

	JOYINFOEX joyInfoEx;

	for (int i=0;((i<16)&&(joyID<0));i++)
	{
		ZeroMemory(&joyInfoEx, sizeof(joyInfoEx));
		joyInfoEx.dwSize = sizeof(joyInfoEx);

		bool joyPresent = (joyGetPosEx(JOYSTICKID1+i, &joyInfoEx) == JOYERR_NOERROR);

		if (joyPresent)
		{
			printf("Joysyick present: %d\n",i);
			joyID = JOYSTICKID1+i;
		}
	}

	if (joyID<0)
	{
		printf("No joysticks found\n");
		return;
	}

	while (1)
	{
		ZeroMemory(&joyInfoEx, sizeof(joyInfoEx));
		joyInfoEx.dwSize = sizeof(joyInfoEx);

		joyInfoEx.dwFlags = JOY_RETURNALL;

		joyGetPosEx(joyID, &joyInfoEx);

		
		printf("x=%05d y=%05d z=%05d r=%05d u=%05d v=%05d hat=%05.1f b=%08X\n",joyInfoEx.dwXpos,joyInfoEx.dwYpos,joyInfoEx.dwZpos,joyInfoEx.dwRpos,joyInfoEx.dwUpos,joyInfoEx.dwVpos, ((short)(joyInfoEx.dwPOV))/100.0f, joyInfoEx.dwButtons);

		Sleep(50);
	}
}